package com.company.project.web;
import com.alibaba.fastjson.JSON;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.Adviser;
import com.company.project.model.ContactMethod;
import com.company.project.service.ContactMethodService;
import com.company.project.service.impl.ContactMethodServiceImpl;
import com.company.project.util.FileUploadUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
* Created by CodeGenerator on 2018/10/08.
*/
@RestController
@RequestMapping("/contactmethod")
public class ContactMethodController {
    @Resource
    @Autowired
    private ContactMethodServiceImpl contactMethodService;

    @Resource
    private FileUploadUtil fileUploadUtil;

    @RequestMapping(value = "/selectContactMethod",method = RequestMethod.GET)
    @ResponseBody
    public List<Map<String,Object>> selectContactMethod(){
        List<Map<String, Object>> maps = contactMethodService.selectContactMethod();
        return maps;
    }

    /**
     * 删除联系方式
     * @param id
     * @return
     */
    @GetMapping ("/delectCMC/{id}")
    @ResponseBody
    public Result delectCon(@PathVariable(value = "id") Integer id){
        Boolean aBoolean = contactMethodService.delectCon(id);
        return ResultGenerator.genSuccessResult(aBoolean);
    }
    /**
     * 修改联系方式
     * @param contactMethod
     * @return
     */
    @PostMapping("/updateContactMethod")
    @ResponseBody
    public Result updateContactMethod(@RequestParam String contactMethod,@RequestParam(required = false) MultipartFile file){
        ContactMethod contactMethod1 = JSON.parseObject(contactMethod, ContactMethod.class);
        if (file!=null){
            String s = fileUploadUtil.writeFile(file, "img");
            if (s == null){
                return ResultGenerator.getInternalServerErrot("图片上传错误");
            }
            contactMethod1.setUri(s);
            Boolean aBoolean = contactMethodService.updateContactMethod(contactMethod1);
            if (aBoolean.equals(true)){
                return ResultGenerator.genSuccessResult(aBoolean);
            }
        }
        Boolean aBoolean = contactMethodService.updateContactMethod(contactMethod1);
        return ResultGenerator.genSuccessResult(aBoolean);
    }

    /**
     * 添加联系方式
     * @param contactMethod
     * @return
     */
    @PostMapping("/insertC")
    @ResponseBody
    public Result insertC(@RequestParam String contactMethod,@RequestParam(required = false) MultipartFile file){
        ContactMethod contactMethod1 = JSON.parseObject(contactMethod, ContactMethod.class);
        if (file!=null){
            String s = fileUploadUtil.writeFile(file, "img");
            if (s == null){
                return ResultGenerator.getInternalServerErrot("图片上传错误");
            }
            contactMethod1.setUri(s);
            Boolean aBoolean = contactMethodService.insertC(contactMethod1);
            if (aBoolean.equals(true)){
                return ResultGenerator.genSuccessResult(aBoolean);
            }
        }
        Boolean aBoolean = contactMethodService.insertC(contactMethod1);
        return ResultGenerator.genSuccessResult(aBoolean);
    }
    @PostMapping("/add")
    public Result add(ContactMethod contactMethod) {
        contactMethodService.save(contactMethod);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        contactMethodService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(ContactMethod contactMethod) {
        contactMethodService.update(contactMethod);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        ContactMethod contactMethod = contactMethodService.findById(id);
        return ResultGenerator.genSuccessResult(contactMethod);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ContactMethod> list = contactMethodService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
